package com.tianshi.user.mapper;

import com.tianshi.entity.UserAddresses;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserAddressMapper {
    // 根据用户id查询用户地址
    @Select("SELECT * FROM user_addresses WHERE user_id = #{userId}")
    List<UserAddresses> searchByUserId(Long userId);
    // 根据地址id删除地址
    @Delete("DELETE FROM user_addresses WHERE address_id = #{addressId}")
    int deleteById(Long addressId);
    //根据地址id查询地址
    @Select("SELECT * FROM user_addresses WHERE address_id = #{addressId}")
    UserAddresses selectByPrimaryKey(Long addressId);
    //更新地址信息
    @Update("UPDATE user_addresses SET receiver_name = #{receiverName}, receiver_phone = #{receiverPhone}, " +
            "province = #{province}, city = #{city}, district = #{district}," +
            "detail_address = #{detailAddress}, is_default = #{isDefault} WHERE address_id = #{addressId}")
    int updateAddress(UserAddresses userAddresses);
    //设置默认地址
    @Update("UPDATE user_addresses SET is_default = 0 WHERE user_id = #{userId}")
    void updateDefault(Long userId);
    //新增地址信息
    @Insert("INSERT INTO user_addresses(user_id, receiver_name, receiver_phone, province, city, district, detail_address, is_default)" +
            " VALUES(#{userId}, #{receiverName}, #{receiverPhone}, #{province}, #{city}, #{district}, #{detailAddress}, #{isDefault}) ")
    int insertAddress(UserAddresses userAddresses);
    //根据地址id查询是否存在该地址
    @Select("SELECT * FROM user_addresses WHERE address_id = #{addressId})")
    UserAddresses searchByAddressId(Long addressId);
}
